// index.js
const { envList } = require('../../envList.js');
import * as API from '../api/request_api.js'
Page({
  data: {
    roomNum :"123",
    deviceNum:0,
    array: [],
    loginFlag:API.loginFlag.login_success,
  },

  onLoad :function (options){
    let that = this
    wx.getStorage({
      key: "key",
      success(res) {
        console.log("getStorage:",res.data)
        that.setData({
          text_name: res.data.userName,
        })
        wx.request({ // 使用post请求，讲用户账号密码发送给服务器储存
          url: API.myURL_getDevice_post, 
          method:'POST', // 请求方式POST
          data: {
            "userID":res.data.userID, // 用户输入的账号
            "password":res.data.password, // 用户输入的密码
          },
          header: API.header,
          success(res) { 
            console.log(res.data[0])
            if(res.data[0].loginFlag == API.loginFlag.login_success){
              console.log("登录成功")
              console.log(res.data[0].deviceList)
              that.setData({
                deviceNum:res.data[0].deviceNum,
                array:res.data[0].deviceList,
              })
            }else{
              console.log("数据错误")
              API.ShowToast("数据错误!", 2000, "error")
            }
          },
          fail(res){
            API.ShowToast("服务器死啦!", 2000, "error")
          }
        })
      },
      fail(res){
        console.log(res.data)
        API.ShowToast("未注册", 2000, "error")
      }
    })
  },

  buttonMyhoume: function(event){
    var that = this
    console.log("设备按钮");
    let value= event.currentTarget.dataset.value
    console.log("value："+ value)
    if(value.deviceType == "台灯"){
      wx.navigateTo({
        url: '../DlightMode1/DlightMode1?deviceID=' + value.deviceType + '&deviceName=' + value.name + '&deviceOnline=' + value.deviceOnline
      })
    }else if(value.deviceType == "开关"){
      wx.navigateTo({
        url: '../DkeyMode1/DkeyMode1?deviceID=' + value.deviceID + '&deviceName=' + value.deviceName + '&deviceOnline=' + value.deviceOnline
      })
    }else if(value.deviceType == "未命名设备"){
      console.log("未定义设备错误");
      wx.showToast({
        title: "未定义设备",
        duration: 1000
      })
    }
  },
});
